package com.guaoran.commons.swagger.ui.filter;

import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author : 孤傲然
 * @description : 替换 URI 链接中重复的 /
 * @date :2020/8/5 12:46
 */
@Component
public class UriFormatFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String newUri = uri.replace("//","/");
        request = new HttpServletRequestWrapper(request){
            @Override
            public String getRequestURI() {
                return newUri;
            }
        };
        filterChain.doFilter(request, response);
    }
}
